package Application.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import DomainData.MainCourse;
import Reservation.MysqlConnection;

public class OrderDAO {
	static Connection connection = null;
	static ResultSet rs = null;
	
	
	public OrderDAO(){
		
	}
	
	public static boolean createOrder(int reservationID, String content){
		
		String[] contentList = content.split(",");
		String deletesql = "DELETE FROM maincourseforreservation"+" WHERE reservationID ="+reservationID;
		PreparedStatement prestate = null;
		String sql = "INSERT INTO maincourseforreservation (mainCourseID,reservationID)"+"VALUES(?,?)";
		try {
			
			
			if(connection!=null){
				connection = MysqlConnection.getConnection();
				prestate = connection.prepareStatement(deletesql);
				prestate.executeUpdate();
				for(String i:contentList){
					
					prestate = connection.prepareStatement(sql);
					int maincourseID = Integer.parseInt(i);
					prestate.setInt(1,maincourseID );
					prestate.setInt(2, reservationID);
					prestate.executeUpdate();
				} 
			}
			else
			{
				System.out.println("**************connection is null******************");
			}
			
				connection.close();
			}
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return true;
		
	}
	public static String getorderList(int reservationID){
		String olist = "";
		Statement statement = null;
		rs = null;
		String sql = "select mainCourseID FROM maincourseforreservation"+" WHERE reservationID ="+reservationID;
		try {
			connection = MysqlConnection.getConnection();
			if(connection!=null){
				statement = connection.createStatement();
				rs = statement.executeQuery(sql);
				while(rs.next()){
					int cid= rs.getInt(1);
					olist+=cid+",";
					System.out.println(olist);
				}
				
			}
			else
			{
				System.out.println("**************connection is null******************");
			}
			
				connection.close();
			}
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!olist.isEmpty())
			olist = olist.substring(0,olist.lastIndexOf(',')).trim();
		return olist;
	}
	public static void updateOrder(int reservationID, String content){
		PreparedStatement prestatement = null;
		String sql = "update maincourseforreservation"+"set orderContent = "+content+"where reservationID = "+reservationID;
		try {
			connection = MysqlConnection.getConnection();
			if(connection!=null){
				prestatement = connection.prepareStatement(sql);
				prestatement.executeUpdate();
			}
			else
			{
				System.out.println("**************connection is null******************");
			}
			
				connection.close();
			}
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static boolean createOrderWithOption(int reservationID, String content,String option){
		
		boolean valid = false;
		String[] contentList = content.split(",");
		String[] optionList = option.split(",");
		String deletesql = "DELETE FROM maincourseforreservation"+" WHERE reservationID ="+reservationID;
		PreparedStatement prestate = null;
		String sql = "INSERT INTO maincourseforreservation(mainCourseID,reservationID,`option`) "+"VALUES(?,?,?)";
		try {
			
			
			if(connection!=null){
				connection = MysqlConnection.getConnection();
				prestate = connection.prepareStatement(deletesql);
				prestate.executeUpdate();
				for(int i=0;i<contentList.length;i++){
					
					prestate = connection.prepareStatement(sql);
					int maincourseID = Integer.parseInt(contentList[i]);
					String optionItem = optionList[i];
					prestate.setInt(1,maincourseID );
					prestate.setInt(2, reservationID);
					if(optionItem.equals(""))
						optionItem = "Nothing";
					prestate.setString(3, optionItem);
				
					prestate.executeUpdate();
				} 
			}
			else
			{
				System.out.println("**************connection is null******************");
			}
			
				connection.close();
				valid = true;
			}
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return valid;
	}
	
	
	

}
